package org.eclipse.football;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DefStatsServlet extends HttpServlet implements Servlet {
	// Override doPost function
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Read in information submitted by form
		String def_tds_raw = request.getParameter("def_tds");
		String picks_raw = request.getParameter("picks");
		String fumbles_raw = request.getParameter("fumbles");
		String sacks_raw = request.getParameter("sacks");
		String blocks_raw = request.getParameter("blocks");
		String safeties_raw = request.getParameter("safeties");
		String pa_raw = request.getParameter("pa");
		String ya_raw = request.getParameter("ya");
		
		// Convert all input values to integers
		int def_tds = stringToInt(def_tds_raw);
		int picks = stringToInt(picks_raw);
		int fumbles = stringToInt(fumbles_raw);
		int sacks = stringToInt(sacks_raw);
		int blocks = stringToInt(blocks_raw);
		int safeties = stringToInt(safeties_raw);
		int pa = stringToInt(pa_raw);
		int ya = stringToInt(ya_raw);
		
		// Obtain existing league
		League league = (League) request.getSession(true).getAttribute("league");
		
		// Obtain existing team
		Team team = (Team) request.getSession(true).getAttribute("team");
		
		// Obtain existing defensive player
		DefPlayer player = (DefPlayer) request.getSession(true).getAttribute("def");
		
		// Add new stats to players old stats and set them in player
		player.setDef_tds(def_tds+player.getDef_tds());
		player.setPicks(picks+player.getPicks());
		player.setFumbles(fumbles+player.getFumbles());
		player.setSacks(sacks+player.getSacks());
		player.setBlocks(blocks+player.getBlocks());
		player.setSafeties(safeties+player.getSafeties());
		player.setPa(pa+player.getPa());
		player.setYa(ya+player.getYa());

		// Set player back to null
		player = null;
		
		// Write the state of the league out to a file
		league.writeState("league.txt");
		
		// Set league in the session
		request.getSession().setAttribute("league", league);
		
		// Set team in the session
		request.getSession().setAttribute("team", team);
		
		// Set player in the session
		request.getSession().setAttribute("deff", player);
		
		// Set off and kicker as null in the session
		request.getSession().setAttribute("off", null);
		request.getSession().setAttribute("kicker", null);
		
		// Forward control back to stats.jsp
		getServletContext().getRequestDispatcher("/TeamPage.jsp").forward(request, response);
		
		// TODO Auto-generated method stub
		//super.doPost(request, response);
	}

	// Define a wrapper for Integer.parseInt function
	int stringToInt(String in){
		// Check if input string is blank or null
		if(in == null || in.equalsIgnoreCase("")){
			// If so return 0
			return 0;
		}
		
		// Else convert string to int
		else{
			return Integer.parseInt(in);
		}
	}
}

